package y2016.test;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TxtParse {
    /**
     * 一级匹配表达式
     */
    private static String[] regexs1 = {
            "第[零一二三四五六七八九十百千]{1,7}[章集回卷篇][:：]?.*",
            "第\\d{1,7}[章集回卷篇][:：]?.*",
            "[零一二三四五六七八九十百千]{1,7}.*",
            "\\d{1,7}.*",
            ".* +第[零一二三四五六七八九十百千]{1,7}[章集回卷篇][:：]?.*",
            ".* +第\\d{1,7}[章集回卷篇][:：]?.*"
    };

    /**
     * 二级匹配表达式
     */
    private static String[] regexs2 = {
            "[零一二三四五六七八九十百千]{1,7}",
            "[^，。“”…？；―-]{1,10}"
    };


    /**
     * 正则表达式。
     * 一对括号一组，共分四组
     * 说明如下：
     * <p>  (^\n|\r|\r\n) 章节之前是换行符。换行符匹配，一般章节名都是换行的，第一章很可能不换，其余章节除非格式问题都会换行
     * <p>  (.*第)  匹配任意长度字符。需要考虑一些特殊的章节名如：002章，前面没有“第”字(难以实现匹配。。。),或者空白，或者形如：“正文 第XX章 xxx”，“第二卷 xx 第xx回 xxx”
     * <p>  ([零一二三四五六七八九十百千0123456789]{1,7}) 章节序号，可以是[零一二三四五六七八九十百千0123456789]中的任意一个字符组成的，长度为1~7个的字符串
     * <p>  [章节卷集部篇回] 匹配其中任意个字符
     * <p>  (.*) 章节名称匹配
     * <p>  (\n|\r|\r\n) 章节结束后换行匹配
     **/
    private static String[] regexs = {
            "(^\n|\r|\r\n)(.*第)([零一二三四五六七八九十百千0123456789]{1,7})([章节卷集部篇回].*)(\n|\r|\r\n)",
            "(\n|\r|\r\n)(.*第)[零一二三四五六七八九十百千]{1,7}[章节卷集部篇回][:：]?.*"

    };


    public static List<String> parseTxtChapter(StringBuilder testString) {
        List<String> chapters = new ArrayList<>();
        Pattern p = Pattern.compile(regexs[0]);
        Matcher matcher = p.matcher(testString);
//		System.out.println(testString.length());
        while (matcher.find()) {
            //for (int i = 0; i <= matcher.groupCount(); i++) {//捕获的组数
            //System.out.println("group" + i + " : " + matcher.start(i) + " - " + matcher.end(i));
            //System.out.println(matcher.group(i));
            chapters.add(testString.substring(matcher.start() + 2, matcher.end() - 1));
            System.out.println(testString.substring(matcher.start() + 2, matcher.end() - 1));
            //}
        }
        return chapters;
    }

    public static StringBuilder readBookToSB(File file, StringBuilder sb) {
        if (sb == null) {
            sb = new StringBuilder();
        }
        if (!file.exists()) {
            return null;
        }

        FileInputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            is = new FileInputStream(file);
            isr = new InputStreamReader(is, "gbk");
            br = new BufferedReader(isr);
            char[] chs = new char[1024 * 25];
            int len = 0;
            while ((len = br.read(chs)) != -1) {
                sb.append(new String(chs, 0, len));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                isr.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb;

    }

    @Test
    public void main() throws Exception {
        File file = new File("E:/书籍/小说/封神演义.txt");
        StringBuilder sb = new StringBuilder();
        readBookToSB(file, sb);
        parseTxtChapter(sb);

    }
}


